package testcase.app;

import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import api.app.ReportList;
import common.Host;
import common.TestData;
import common.user.User;

/**
 * 帮忙医APP获取用户体检报告接口测试用例
 * @author gaowen
 *
 */

public class TestReportList {
		
	/**
	 * 帮忙医APP获取用户体检报告接口测试环境数据驱动
	 * @return 测试数据
	 * @throws Exception
	 */
	@DataProvider (name = "Test")
	public static String[][] testReportListDataProvider() throws Exception
	{
		Host.setup("Test");
		TestData td = new TestData("app/TestReportList.xml");
		return td.getParam();
	}
	
	/**
	 * 帮忙医APP获取用户体检报告接口生产环境数据驱动
	 * @return 测试数据
	 * @throws Exception
	 */
	@DataProvider (name = "Prod")
	public static String[][] prodReportListDataProvider() throws Exception
	{
		Host.setup("Prod");
		TestData td = new TestData("app/TestReportList.xml");
		return td.getParam();
	}
	
	/**
	 * 帮忙医APP获取用户体检报告接口测试环境测试用例
	 * @param caseName - 用例名称
	 * @param UserAccount -用户账号
	 * @param PassWord - 用户密码
	 * @throws ParseException 
	 * @throws IOException 
	 * @throws JSONException 
	 * @throws SQLException 
	 * @throws ClassNotFoundException 
	 */
	@Test(dataProvider="Test",groups={"Test"})
	public void testReportListTestCase(String caseName,String UserAccount,String PassWord) throws JSONException, IOException, ParseException, ClassNotFoundException, SQLException
	{
		//获取用户token
		User ReptUser=new User(UserAccount, PassWord);
		String token=ReptUser.token;
		//调用ReportList接口并将接口返回字符串转为JSON格式
		String result=ReportList.Options(token);
		JSONObject jsonObject;
		if (result==null || result.isEmpty())
		{
			jsonObject = null;
		}
		else
		{
			jsonObject = new JSONObject(result);
		}
		
		//检查借口返回报告信息是否为用户的报告信息
		int check=0;            //检查参数，初始为0，UserID对上为1，UserID对不上为-1，没有报告为2
		
		JSONArray jsonData=jsonObject.getJSONArray("Data");
		if (jsonData.length()>0)
		{
			for (int i=0;i<jsonData.length();i++)
			{
				jsonObject=jsonData.getJSONObject(i);
				if (jsonObject.getInt("UserID")==ReptUser.id)
				{
					check=1;     //接口返回UserID与用户的UserID一致，将检查参数赋为1
				}
				else
				{
					check=-1;    //接口返回UserID与用户的UserID不一致，将检查参数赋为-1并跳出循环
					break;
				}
			}
		}
		else
		{
			check=2;	//用户无报告
		}
		
		//设置检查点
		switch (caseName) {
		case "TwoReports":
			Assert.assertEquals(jsonData.length(), 2);
			Assert.assertEquals(check, 1);
			break;
		case "OneReport":
			Assert.assertEquals(jsonData.length(), 1);
			Assert.assertEquals(check, 1);
		break;
		
		case "NoReport":
			Assert.assertEquals(jsonData.length(), 0);
			Assert.assertEquals(check, 2);
		break;
		default:
			break;
		}
	}
	
	/**
	 * 帮忙医APP获取用户体检报告接口生产环境测试用例
	 * @param caseName - 用例名称
	 * @param UserAccount -用户账号
	 * @param PassWord - 用户密码
	 * @throws ParseException 
	 * @throws IOException 
	 * @throws JSONException 
	 * @throws SQLException 
	 * @throws ClassNotFoundException 
	 */
	@Test(dataProvider="Prod",groups={"Prod"})
	public void prodReportListTestCase(String caseName,String UserAccount,String PassWord) throws JSONException, IOException, ParseException, ClassNotFoundException, SQLException
	{
		//获取用户token
		User ReptUser=new User(UserAccount, PassWord);
		String token=ReptUser.token;
		//调用ReportList接口并将接口返回字符串转为JSON格式
		String result=ReportList.Options(token);
		JSONObject jsonObject;
		if (result==null || result.isEmpty())
		{
			jsonObject = null;
		}
		else
		{
			jsonObject = new JSONObject(result);
		}
		
		//检查借口返回报告信息是否为用户的报告信息
		int check=0;            //检查参数，初始为0，UserID对上为1，UserID对不上为-1，没有报告为2
		
		JSONArray jsonData=jsonObject.getJSONArray("Data");
		if (jsonData.length()>0)
		{
			for (int i=0;i<jsonData.length();i++)
			{
				jsonObject=jsonData.getJSONObject(i);
				if (jsonObject.getInt("UserID")==ReptUser.id)
				{
					check=1;     //接口返回UserID与用户的UserID一致，将检查参数赋为1
				}
				else
				{
					check=-1;    //接口返回UserID与用户的UserID不一致，将检查参数赋为-1并跳出循环
					break;
				}
			}
		}
		else
		{
			check=2;	//用户无报告
		}
		
		//设置检查点
		switch (caseName) {
		case "MyReports":
			Assert.assertEquals(jsonData.length(), 1);
			Assert.assertEquals(check, 1);
			break;
		default:
			break;
		}
	}
}
